/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.master;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.SystemTray;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import programsettings.ProgramSettings;

import com.melloware.jintellitype.JIntellitype;


public class SoftStarterGUI extends JFrame {

    public static final String FIRESOFT_VERSION = "Firesoft 1.4";
    public static boolean useWindowsHotkey = true;
    public static boolean useSystemTray = true;
    
	private GUIStateController stateController;
	private BorderLayout layout;
	private JPanel masterContainer;
	private JPanel basicView;
	private JPanel centreView;
	
    public SoftStarterGUI() {
    	
        setUpWindow();
        setNimbusLookAndFeel();
        checkIfOperationsAreSupported();
        if(useWindowsHotkey) 
        	loadCorrectVersionOfJIntellitypeDLL();
        
        build();
        buildGUI();
        
        setVisible(true);
    }
   
    private void setUpWindow() {
    	
        this.setTitle("Firesoft");
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setAlwaysOnTop(ProgramSettings.getInstance().isAlwaysOnTop());
        setUndecorated(ProgramSettings.getInstance().getShowWindowDecorations());
        
    }

    private void setNimbusLookAndFeel() {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                System.out.println(info.getName());
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    
//                    UIManager.put("control", new Color(60, 68, 83));
//                    UIManager.put("info", new Color(242, 242, 189));
//                    UIManager.put("nimbusBase", new Color(1, 1, 1));
//                    UIManager.put("nimbusDisabledText", new Color(247, 247, 255));
//                    UIManager.put("nimbusFocus", new Color(16, 170, 195));
//                    UIManager.put("nimbusGreen", new Color(213, 218, 17));
//                    UIManager.put("nimbusInfoBlue", new Color(40, 78, 156));
//                    UIManager.put("nimbusLightBackground", new Color(255, 255, 255));
//                    UIManager.put("nimbusOrange", new Color(230, 217, 204));
//                    UIManager.put("nimbusRed", new Color(169, 46, 34));
//                    UIManager.put("nimbusSelectedText", new Color(251, 244, 244));
//                    UIManager.put("nimbusSelectionBackground", new Color(57, 105, 138));
//                    UIManager.put("text", new Color(129, 109, 109));

                    break;
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
	private void build() {

		stateController = new GUIStateController(this);
		masterContainer = new GradientPanel(Color.WHITE, Color.WHITE);
    	basicView = new JPanel();
    	centreView = new JPanel();
    	
    }
   
    private void buildGUI() {
    	
    	layout = new BorderLayout();
    	masterContainer.setLayout(layout);
        masterContainer.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.BLACK), BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    	basicView.add(stateController.getCommandLineView());
    	basicView.add(stateController.getControllerButtonsView());
    	basicView.setOpaque(false);
    	
    	centreView.setOpaque(false);
    	
    	masterContainer.add(basicView, BorderLayout.NORTH);
    	add(masterContainer);
    	
    	pack();
	}
        
    private void checkIfOperationsAreSupported() {
    	
        System.out.println(System.getProperty("os.arch") + " " + System.getProperty("os.version"));
        if(!Desktop.isDesktopSupported() || !SystemTray.isSupported()) {
            JOptionPane.showMessageDialog(this,
                    "Your operating environment doesn't support the operations of this program.",
                    "Sorry",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }
    
    private void loadCorrectVersionOfJIntellitypeDLL() {
        extractResource("/resources/JIntellitype64.dll", new File("JIntellitype64.dll"));
        extractResource("/resources/JIntellitype.dll", new File("JIntellitype.dll"));
        
        if(System.getProperty("os.arch").contains("64"))
            JIntellitype.setLibraryLocation("JIntellitype64.dll");
        else
            JIntellitype.setLibraryLocation("JIntellitype.dll");
    }
    
    public String extractResource(String resourceName, File destinationFile) {
        try {
           InputStream resource = getClass().getResourceAsStream(resourceName);
           FileOutputStream outStream = new FileOutputStream(destinationFile);
           byte[] buffer = new byte[1024];
           int bytes;
           while ((bytes = resource.read(buffer)) != -1) {
              outStream.write(buffer, 0, bytes);
           }
           outStream.close();
           resource.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        System.out.println("Successfully extracted "+ destinationFile.getAbsolutePath());
        return destinationFile.getAbsolutePath();
     }

    public void showSettingsView(JPanel settingsView) {
        
        centreView.add(settingsView);
        masterContainer.add(centreView, BorderLayout.CENTER);
        
    }
    
    public void hideSettingsView() {
        
        centreView.removeAll();
        masterContainer.remove(centreView);
    }
    
	/**
     * Starts the program.
     * 
     * @param args
     */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
				new SoftStarterGUI();
		    }
		});
	}

}
